package com.itttq.kb.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.kb.model.to.PageParamTo;
import com.itttq.kb.model.po.TopicPo;
import com.itttq.kb.mapper.ITopicMapper;

@Service
public class TopicService {
	@Autowired
	private ITopicMapper topicMapper;
	
	/**
	 * 根据ID获取对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return TopicPo
	 */
	public TopicPo getTopic(int id) {
		return topicMapper.getTopic(id);
	}
	/**
	 * 获取最新主题
	 * @Author 迷蝶
	 * @return List<TopicPo>
	 */
	public List<TopicPo> getLatestTopics(Integer catId, int size) {
		return topicMapper.getTopTopics("update_time", catId, size);
	}

	/**
	 * 获取最热议主题
	 * @Author 迷蝶
	 * @return List<TopicPo>
	 */
	public List<TopicPo> getMostReply10Topic(Integer catId, int size) {
		return topicMapper.getTopTopics("number_reply", catId, size);
	}

	/**
	 * 获取查看最多主题
	 * @Author 迷蝶
	 * @return List<TopicPo>
	 */
	public List<TopicPo> getMostRead10Topic(Integer catId, int size) {
		return topicMapper.getTopTopics("number_read", catId, size);
	}
	/**
	 * 获取主体列表，按更新时间排序
	 * 
	 * @param topic
	 * @param ppt
	 * @return
	 * @Author 迷蝶
	 * @return List<TopicPo>
	 */
	public List<TopicPo> getTopics(TopicPo topic, PageParamTo ppt) {
		return topicMapper.getTopics(topic.getTitle(), topic.getIsTop(), topic.getCategoryId(), ppt.getStart(), ppt.getSize());
	}
	
	/**
	 * 更新主题阅读数
	 * 
	 * @param id
	 * @Author 迷蝶
	 * @return void
	 */
	public int updateRead(int id) {
		return topicMapper.updateNumber("read", id);
	}
	
	/**
	 * 更新主题回复数
	 * 
	 * @param id
	 * @Author 迷蝶
	 * @return void
	 */
	public int updateReply(int id) {
		return topicMapper.updateNumber("reply", id);
	}

	public int getTopicCount(TopicPo topic) {
		return topicMapper.getCount(topic);
	}

	public int delTopic(int id) {
		TopicPo topic = new TopicPo();
		topic.setId(id);
		return topicMapper.delete(topic);
	}

	public void addTopic(TopicPo topic) {
		topicMapper.insert(topic);
	}
	
	public void updateTopic(TopicPo topic) {
		topicMapper.update(topic);
	}
}
